
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module config-manager
* @author: Hjava
* @description: 用户信息管理模块
* @since: 2018-07-23 12:56:26
*/
var promise_1 = __webpack_require__(/*! _util/promise */ "./src/core/utils/promise.ts");
var map_1 = __webpack_require__(/*! _Eutils/map */ "./src/extra/utils/map.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/extra/utils/log.ts");
var EmoticonManager = /** @class */ (function () {
    function EmoticonManager() {
        this._emoticonMap = new map_1.default();
        this.mtdx = {};
    }
    EmoticonManager.prototype.init = function (mtdx) {
        log_1.default.log('EmoticonManager::init');
        this.mtdx = mtdx;
    };
    EmoticonManager.prototype.saveEmoticonPackages = function (packages) {
        var _this = this;
        var keys = Object.keys(packages);
        keys.forEach(function (element) {
            var emotionPackage = packages[element];
            if (emotionPackage && Object.keys(emotionPackage).length > 0) {
                var stickersMap_1 = new map_1.default();
                var stickers = emotionPackage.stickers.map(function (sticker) {
                    return __assign(__assign({}, sticker), { packageId: element, packageName: emotionPackage.name, thumbURL: emotionPackage.baseUrl + sticker.thumbMediaId, primaryURL: emotionPackage.baseUrl + sticker.primaryMediaId });
                });
                stickers.forEach(function (sticker) {
                    stickersMap_1.set(sticker.stickerId, sticker);
                });
                var value = __assign(__assign({}, emotionPackage), { iconURL: emotionPackage.baseUrl + emotionPackage.iconMediaId, stickers: stickers,
                    stickersMap: stickersMap_1 });
                _this._emoticonMap.set(element, value);
            }
        });
    };
    /**
     * 从服务端拉取最新的自定义表情包,并且存储到本地
     *
     * @param {*} packageIds
     * @returns
     * @memberof EmoticonsManager
     */
    EmoticonManager.prototype.fetchEmoticonPackages = function (packageIds) {
        var _this = this;
        return new promise_1.default(function (resolve, reject) {
            var params = {};
            packageIds.forEach(function (element) {
                params[element] = {};
            });
            var dataObject = {
                packages: params
            };
            log_1.default.info('EmoticonManager::fetchEmoticonPackages info:' + JSON.stringify(dataObject));
            _this.mtdx.HTTP.Common.getEmoticonsPackages({
                data: dataObject,
                success: function (_a) {
                    var rescode = _a.rescode, data = _a.data;
                    if (rescode === 0 && data.packages) {
                        _this.saveEmoticonPackages(data.packages);
                        resolve(packageIds.map(function (packageId) {
                            var _a;
                            return _a = {},
                                _a[packageId] = _this.getEmoticonPackage({ packageId: packageId }),
                                _a;
                        }));
                    }
                    else {
                        log_1.default.info('EmoticonManager::fetchEmoticonPackages fail info:' + rescode);
                        reject(rescode, data);
                    }
                },
                error: function (e) {
                    log_1.default.info('EmoticonManager::fetchEmoticonPackages fail info:' + e);
                    reject(e);
                }
            });
        });
    };
    EmoticonManager.prototype.getEmoticonPackage = function (_a) {
        var packageId = _a.packageId;
        if (this._emoticonMap.has(packageId)) {
            return this._emoticonMap.get(packageId);
        }
        return null;
    };
    EmoticonManager.prototype.fetchEmoticonSticker = function (_a) {
        var _this = this;
        var packageId = _a.packageId, stickerId = _a.stickerId;
        return new promise_1.default(function (resolve, reject) {
            var dataObject = {
                packageId: packageId,
                stickerId: stickerId
            };
            log_1.default.info('EmoticonManager::fetchEmoticonSticker info:' + JSON.stringify(dataObject));
            _this.mtdx.HTTP.Common.getOneEmoticonsSticker({
                data: dataObject,
                success: function (_a) {
                    var rescode = _a.rescode, data = _a.data;
                    if (rescode === 0 && data.package) {
                        var emotionPackage = data.package;
                        resolve(__assign(__assign({}, emotionPackage), { iconURL: emotionPackage.baseUrl + emotionPackage.iconMediaId, sticker: __assign(__assign({}, emotionPackage.sticker), { packageId: packageId, packageName: emotionPackage.name }) }));
                    }
                    else {
                        log_1.default.info('EmoticonManager::fetchEmoticonSticker fail info:' + rescode);
                        reject({ rescode: rescode, data: data });
                    }
                },
                error: function (e) {
                    log_1.default.info('EmoticonManager::fetchEmoticonSticker fail info:' + e);
                    reject(e);
                }
            });
        });
    };
    EmoticonManager.prototype.getEmoticonSticker = function (_a) {
        var packageId = _a.packageId, stickerId = _a.stickerId;
        var emotionPackage = this._emoticonMap.get(packageId);
        if (emotionPackage && emotionPackage.stickersMap.has(stickerId)) {
            var result = __assign({}, this._emoticonMap.get(packageId));
            delete result.stickers;
            delete result.stickersMap;
            delete result.ver;
            return __assign(__assign({}, result), { sticker: emotionPackage.stickersMap.get(stickerId) });
        }
        else {
            log_1.default.info('EmoticonManager::getEmoticonSticker fail info:' + stickerId);
            return null;
        }
    };
    return EmoticonManager;
}());
exports.default = new EmoticonManager();


//# sourceURL=webpack://MTDX/./src/extra/managers/emoticon-manager.ts?